package edu.rit.drh4818.volume;

import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.WakeupOr;


/**
 * Behavior which calls update on the volume at set intervals.
 * 
 * @author Darren Headrick
 *
 */
public class AnimationBehavior extends Behavior
{
	/**
	 * Pause the animation.
	 */
	public static boolean PAUSE = false; 
	
	/**
	 * The volume renderer.
	 */
	private VolumeRenderer renderer;
    
	/**
	 * List of conditions for wakeup.
	 */
    private WakeupCondition conditions ;

    /**
     * The volume this behavior should update.
     */
    private Volume volume;
    
    /**
     * Create a new animation behavior.
     * @param delay Time to sleep in between renders.
     * @param volume Volume to update.
     */
    public AnimationBehavior( int delay, Volume volume )
    {
    	this.volume = volume;
    	WakeupCriterion criterion[] = { new WakeupOnElapsedFrames( delay) };
    	conditions = new WakeupOr( criterion );
    }
    
    /**
     * Setup animation behavior.
     * @param renderer
     */
    public void initialize( VolumeRenderer renderer )
    {
    	this.renderer = renderer;
    }
    
    /**
     * Start the wakeup loop.
     */
    public void initialize() 
    {
    	if( volume.animated )
    		wakeupOn(conditions);
    }
    
    
    /**
     * if not paused, update the volume data and renderer.
     * Continue the loop.
     */
    @SuppressWarnings("unchecked")
	public void processStimulus( Enumeration criteria ) 
    {
    	if( renderer != null && !PAUSE )
		{
			volume.update();
			renderer.update();
		}
    	wakeupOn( conditions );
	}
}
